package com.mc.web.controller.lab;

import java.util.List;

import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mc.common.annotation.Log;
import com.mc.common.core.controller.BaseController;
import com.mc.common.core.domain.AjaxResult;
import com.mc.common.enums.BusinessType;
import com.mc.lab.domain.LabExpert;
import com.mc.lab.service.ILabExpertService;
import com.mc.common.utils.poi.ExcelUtil;
import com.mc.common.core.page.TableDataInfo;

/**
 * 实验室专家Controller
 *
 * @author mc
 * @date 2021-03-18
 */
@RestController
@RequestMapping("/lab/expert")
public class LabExpertController extends BaseController
{
    @Autowired
    private ILabExpertService labExpertService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询实验室专家列表
     */
    @PreAuthorize("@ss.hasPermi('lab:expert:list')")
    @GetMapping("/list")
    public AjaxResult list(LabExpert labExpert)
    {
        //startPage();
        List<LabExpert> list = labExpertService.selectLabExpertList(labExpert);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('lab:expert:list')")
    @GetMapping("/page/list")
    public TableDataInfo pageList(LabExpert labExpert)
    {
        startPage();
        List<LabExpert> list = labExpertService.selectLabExpertList(labExpert);
        return getDataTable(list);
    }

    /**
     * 导出实验室专家列表
     */
    @PreAuthorize("@ss.hasPermi('lab:expert:export')")
    @Log(title = "实验室专家", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabExpert labExpert)
    {
        List<LabExpert> list = labExpertService.selectLabExpertList(labExpert);
        ExcelUtil<LabExpert> util = new ExcelUtil<LabExpert>(LabExpert.class);
        return util.exportExcel(list, "expert");
    }

    /**
     * 获取实验室专家详细信息
     */
    @PreAuthorize("@ss.hasPermi('lab:expert:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(labExpertService.selectLabExpertById(id));
    }

    /**
     * 新增实验室专家
     */
    @PreAuthorize("@ss.hasPermi('lab:expert:add')")
    @Log(title = "实验室专家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabExpert labExpert)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        labExpert.setCreateUserId(loginUser.getUsername());
        labExpert.setCreateTime(DateUtils.getNowDate());
        labExpert.setState("0");
        return toAjax(labExpertService.insertLabExpert(labExpert));
    }

    /**
     * 修改实验室专家
     */
    @PreAuthorize("@ss.hasPermi('lab:expert:edit')")
    @Log(title = "实验室专家", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabExpert labExpert)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        labExpert.setUpdateUserId(loginUser.getUsername());
        labExpert.setUpdateTime(DateUtils.getNowDate());
        return toAjax(labExpertService.updateLabExpert(labExpert));
    }

    /**
     * 删除实验室专家
     */
    @PreAuthorize("@ss.hasPermi('lab:expert:remove')")
    @Log(title = "实验室专家", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(labExpertService.deleteLabExpertById(id));
    }
}